package main

import (
	"api/common"
	"io"
	"log"
	"net/http"
	"yunion.io/x/onecloud/pkg/mcclient"
)

func getToken() (mcclient.TokenCredential, error) {
	client := mcclient.NewClient(
		"https://api.saltyun.com/api/s/identity/v3",
		60,
		true,
		true,
		"",
		"",
	)
	token, err := client.Authenticate("sysadmin", "N48XSRmcsP5YMcPf", "Default", "system", "Default")
	return token, err
}

func main() {
	req, err := http.NewRequest("GET", "https://api.saltyun.com/api/s/identity/v3/roles", nil)
	if err != nil {
		panic(err)
	}

	token, err := getToken()
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Set("X-Auth-Token", token.GetTokenString())
	req.Header.Set("Content-Type", "application/json")

	clientHttp := &http.Client{}
	resp, err := clientHttp.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)

	common.OutputJson(string(body))
}
